package com.project.common.utils;

import java.io.*;


import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.net.URL;
import java.net.URLDecoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
public class Serial {
//    将流水号存储到properties文件中
        public void addUser(String name,String pre) throws FileNotFoundException, UnsupportedEncodingException {
            FileInputStream fis = null;

            URL resource = Serial.class.getResource("/num.properties");
            String url = resource.getPath();
            url= URLDecoder.decode(url,"utf-8");
//            System.out.println(url);
            File file = new File(url);

            Properties pro = new Properties();
//            用程序对properties做修改，java培训先将properties加载到内存中

            try {
            fis = new FileInputStream(file);//初始化输入流

            } catch (FileNotFoundException e) {
            e.printStackTrace();

            }

            try {
            pro.load(fis); //加载

            } catch (IOException e) {
            e.printStackTrace();

            }

            if(pre.equals("100")){
                if(pro.get(name)==null||pro.get(name)==""){
                    pro.setProperty(name,"1000001");
                }else{
                    Integer val = Integer.valueOf(pro.get(name)+"");
                    val++;
                    pro.setProperty(name,val+"");
                }
            }else{
                if(pro.get(name)==null||pro.get(name)==""){
                    pro.setProperty(name,"10001");
                }else{
                    Integer val = Integer.parseInt(pro.get(name)+"");
                    val++;
//                    System.out.println(val);
                    pro.setProperty(name,val+"");
                }
            }


            FileOutputStream fos = null;

            try {
                fos = new FileOutputStream(file); //初始化一个输出流


            } catch (FileNotFoundException e) {
                e.printStackTrace();

            }

            try {
                pro.store(fos,""); //写回硬盘

            } catch (IOException e) {
                e.printStackTrace();

            }

            try {
                fis.close();

                fos.close();

            } catch (IOException e) {
                e.printStackTrace();

            }

        }
//        获取并拼接编号
        public String getNumber(String name,String pre,String fid,String sid,String tid) throws IOException {
            URL resource = Serial.class.getResource("/num.properties");
            String url = resource.getPath();
            url= URLDecoder.decode(url,"utf-8");
//            System.out.println(url);
            File file = new File(url);
//           获取流水号
            Properties prop = new Properties();
            prop.load(new FileInputStream(file));
            String waterNum = (String) prop.get(name);

//            获取当前时间
            Date date = new Date();
            SimpleDateFormat format=new SimpleDateFormat("yyyyMMdd");
            String time = format.format(date);

//            拼接编号
            String num = null;
            if(pre.equals("100")){
//                截取流水号
                waterNum =waterNum.substring(1);

                num = pre+fid+sid+tid+waterNum;
            }else{
//                System.out.println(waterNum);
                waterNum = waterNum.substring(1);
                num = pre+time+waterNum;
            }
            return num;
        }

        public static void main(String[] args) throws IOException  {

            Serial num = new Serial();
            num.addUser("product_cd","100");
            String product_id = num.getNumber("product_cd", "100", "01", "02", "01");
            System.out.println(product_id);

        }



}
